package com.zl.learning.thread;

public class TestSync {
    public static class User {
        int age;
    }

    private void add1(User user) {
        synchronized (user) {
            user.age++;
        }
    }

    private void add2(User user) {
        synchronized (user) {
            user.age++;
        }
    }

    private void add3(User user) {
        synchronized (user) {
            user.age++;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        TestSync testSync = new TestSync();
        User u = new User();
        u.age = 0;
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 10000; i++) {
                testSync.add1(u);
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 10000; i++) {
                testSync.add1(u);
            }
        });
        Thread t3 = new Thread(()->{
            for (int i = 0; i < 10000; i++) {
                testSync.add1(u);
            }
        });
        t1.start();
        t2.start();
        t3.start();
        t1.join();
        t2.join();
        t3.join();
        System.out.println(u.age);
    }
}
